package com.rush.springbootinit.manager;

import cn.hutool.core.collection.CollUtil;
import com.rush.springbootinit.exception.BusinessException;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionChoice;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
import com.volcengine.ark.runtime.service.ArkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class AiManager {
    @Autowired
    private ArkService service;
    private static String MODLES = "deepseek-r1-250120";
    public String outQuestionAi(String questionCount , String systemPrompt){
        final List<ChatMessage> messages = new ArrayList<>();
        final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content(systemPrompt).build();
        final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content(questionCount).build();
        messages.add(userMessage);
        messages.add(systemMessage);
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model(MODLES)
                .messages(messages)
                .build();
        List<ChatCompletionChoice> choices = service.createChatCompletion(chatCompletionRequest).getChoices();
        if (CollUtil.isNotEmpty(choices)){
            return (String) choices.get(0).getMessage().getContent();
        }        // shutdown service after all requests is finished
        service.shutdownExecutor();
        throw new BusinessException(500, "ai操作失败");

    }







}
